package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.Resource;
import com.zyh.entity.ResultEntity;
import com.zyh.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-08
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private IResourceService resourceService;
    //展示主页面中左侧菜单
    @GetMapping("/showUserMenu")
    public ResultEntity<List<Resource>> showUserMenu(){
        // TODO: 2022/5/8 从请求头里获取用户ID
        int adminId= 3;
        return ResultEntity.ok(resourceService.selectResourcesByAdminId(adminId));
    }
    //获取权限树
    @GetMapping("/showAllResourceTree")
    public ResultEntity<List<Resource>> showAllResourceTree(){
        return ResultEntity.ok(resourceService.selectMenu());
    }

    //权限列表展示
    @GetMapping("/selectAll")
    public ResultEntity<List<Resource>> selectAll(){
        return ResultEntity.ok(resourceService.list());
    }

    //根据权限标记查找权限信息
    @GetMapping("/selectOne/{resourceId}")
    public ResultEntity<Resource> selectOne(@PathVariable("resourceId") Integer resourceId){
        return ResultEntity.ok(resourceService.getById(resourceId));
    }

    //修改权限信息
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Resource resource){
        return resourceService.updateById(resource)==true ? ResultEntity.ok() : ResultEntity.error("修改失败！");
    }

    //根据菜单标记查找该菜单下面的权限点
    @GetMapping("/selectChildrenByPid/{resourceId}")
    public ResultEntity<List<Resource>> selectChildrenByPid(@PathVariable("resourceId") Integer resourceId){
        return ResultEntity.ok(resourceService.list(new QueryWrapper<Resource>().eq("parent_id",resourceId)));
    }
}

